package game;

import java.io.File;
import java.util.Date;
import java.util.Random;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

import util.CommonUtil;
import Public.ServerMoney;

public class JackPot extends YuanActivityBase{
	/**
	 * 实例化奖池
	 */
	 public JackPot(String xmlUrl)
     {
		 super();
         commission = 0.1f;
         lotteryOdds = 10000;
         ran = new Random(CommonUtil.currentMillis());
         needBlood = 10;
         File file=new File(xmlUrl);
         ReadXML(file);
     }
	 

	 
	 protected File xmlFile;
	 protected XMLConfiguration xmlConf;
	 protected void ReadXML(File file)
	 {
		 try
		{
			 xmlFile=file;
			 xmlConf = new XMLConfiguration(file);
			 commission=xmlConf.getFloat("commission");
			 lotteryOdds=xmlConf.getInt("lotteryOdds");
			 needBlood=xmlConf.getInt("needBlood");
			 numPoolBlood=xmlConf.getInt("numPoolBlood");
			 numGMPoolBlood=xmlConf.getInt("numGMPoolBlood");
			
		} catch (ConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 
	 public void Save()
	 {
		 xmlConf.setProperty("commission", commission);
		 xmlConf.setProperty("lotteryOdds", lotteryOdds);
		 xmlConf.setProperty("needBlood", needBlood);
		 xmlConf.setProperty("numPoolBlood", numPoolBlood);
		 xmlConf.setProperty("numGMPoolBlood", numGMPoolBlood);
		 try
		{
			xmlConf.save(xmlFile);
		} catch (ConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	 
	 /**
	  * 随机实例
	  */
	 protected Random ran;
	 
	 /**
	  * 单次抽奖所需血石
	  */
	 protected int needBlood;

	public int getNeedBlood() {
		return needBlood;
	}

	public void setNeedBlood(int needBlood) {
		this.needBlood = needBlood;
	}
	 /**
	  * 开始时间
	  */
	 protected Date timeStart;

	
	public Date getTimeStart() {
		return timeStart;
	}

	public void setTimeStart(Date timeStart) {
		this.timeStart = timeStart;
	}

	public Date getTimeEnd() {
		return timeEnd;
	}

	public void setTimeEnd(Date timeEnd) {
		this.timeEnd = timeEnd;
	}
	/**
	 * 结束时间
	 */
	  protected Date timeEnd;

	
	  /**
	   * 奖池奖金
	   */
    protected int numPoolBlood;
    /**
     *  奖池GM添加的奖金
     */
    protected int numGMPoolBlood;
	public int getNumGMPoolBlood() {
		
		return numGMPoolBlood;
	}

	public void setNumGMPoolBlood(int numGMPoolBlood) {
		this.numGMPoolBlood = numGMPoolBlood;
	}
    /**
     * 奖池总奖金
     */
  
	

	public Integer getPoolBlood()
	{
		if(numPoolBlood+numGMPoolBlood > 2000)
		{
			return (int)((numPoolBlood+numGMPoolBlood)*(1-commission));
		}else{
			return numPoolBlood+numGMPoolBlood;
		}
	}
	
	
	
	
	
	
	
	
    /**
     * 提成比例
     */
	 protected float commission;

	public float getCommission() {
		return commission;
	}

	public void setCommission(float commission) {
		this.commission = commission;
	}
    /**
     * 中奖几率
     */
	   protected int lotteryOdds;

	public int getLotteryOdds() {
		return lotteryOdds;
	}

	public void setLotteryOdds(int lotteryOdds) {
		this.lotteryOdds = lotteryOdds;
	}
	   /**
	    * 设置中奖几率
	    */
	public  void SetLotteryOdds(int mNum){
		 lotteryOdds = mNum;
	}
	/**
	 * 设置提成比例
	 */
	public void SetCommission(float mNum)
    {
        commission = mNum;
    }
	/**
	 * 设置单次抽奖所需血石
	 */
	public void SetNeedBlood(int mNum)
    {
        needBlood = mNum;
    }
	/**
	 * 添加GM奖金
	 */
	  public  void AddGMPoolBlood(int mNum)
      {
          
              numGMPoolBlood += mNum;
              ServerMoney.Out(0, mNum);
          
      }
	/**
	 * 开奖
	 * 开奖血石
	 * 开奖次数
	 */
	  
	  public int TheLottery(int mNumBlood,int mTimes)
       {
              
               numPoolBlood += mNumBlood;
               if (getPoolBlood() < 2000)
               {
                   return 0;
               }
               else
               {
                   int numRan = 0;
                   int numTemp = 0;
                   for (int i = 0; i < mTimes; i++)
                   {
                       numRan = ran.nextInt(lotteryOdds);
                       numTemp =ran.nextInt(lotteryOdds);
//                    		   CommonUtil.Next(0, lotteryOdds);
                       if (numRan == numTemp)
                       {
                           int getNum = getPoolBlood();
                           int serverGet = numGMPoolBlood + numPoolBlood - getNum;
                           ServerMoney.In(0, serverGet,0);
                           numPoolBlood = 0;
                           numGMPoolBlood = 0;
                           
                           return getNum;
                       }
                   }
                   return 0;
               }
           
       }
	
}
